home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / opcodes / sparc-dis.c < prev    next >
C/C++ Source or Header  |  1994-10-05  |  19KB  |  711 lines

  1. /* Print SPARC instructions.
  2.    Copyright 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "ansidecl.h"
  19. #include "opcode/sparc.h"
  20. #include "dis-asm.h"
  21. #include <string.h>
  22.  
  23. /* Sign-extend a value which is N bits long.  */
  24. #define    SEX(value, bits) \
  25.     ((((int)(value)) << ((8 * sizeof (int)) - bits))    \
  26.              >> ((8 * sizeof (int)) - bits) )
  27.  
  28. static  char *reg_names[] =
  29. { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",    
  30.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",    
  31.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",    
  32.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",    
  33.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",    
  34.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",    
  35.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  36.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  37. #ifndef NO_V9
  38.   "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",    
  39.   "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47",    
  40.   "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55",
  41.   "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63",
  42. /* psr, wim, tbr, fpsr, cpsr are v8 only.  */
  43. #endif
  44.   "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr"
  45. };
  46.  
  47. #define    freg_names    (®_names[4 * 8])
  48.  
  49. #ifndef NO_V9
  50. /* These are ordered according to there register number in
  51.    rdpr and wrpr insns.  */
  52. static char *v9_priv_reg_names[] =
  53. {
  54.   "tpc", "tnpc", "tstate", "tt", "tick", "tba", "pstate", "tl",
  55.   "pil", "cwp", "cansave", "canrestore", "cleanwin", "otherwin",
  56.   "wstate", "fq"
  57.   /* "ver" - special cased */
  58. };
  59. #endif
  60.  
  61. /* Macros used to extract instruction fields.  Not all fields have
  62.    macros defined here, only those which are actually used.  */
  63.  
  64. #define X_RD(i) (((i) >> 25) & 0x1f)
  65. #define X_RS1(i) (((i) >> 14) & 0x1f)
  66. #define X_LDST_I(i) (((i) >> 13) & 1)
  67. #define X_ASI(i) (((i) >> 5) & 0xff)
  68. #define X_RS2(i) (((i) >> 0) & 0x1f)
  69. #define X_IMM13(i) (((i) >> 0) & 0x1fff)
  70. #define X_DISP22(i) (((i) >> 0) & 0x3fffff)
  71. #define X_IMM22(i) X_DISP22 (i)
  72. #define X_DISP30(i) (((i) >> 0) & 0x3fffffff)
  73.  
  74. #ifndef NO_V9
  75. #define X_DISP16(i) (((((i) >> 20) & 3) << 14) | (((i) >> 0) & 0x3fff))
  76. #endif
  77.  
  78. /* Here is the union which was used to extract instruction fields
  79.    before the shift and mask macros were written.
  80.  
  81.    union sparc_insn
  82.      {
  83.        unsigned long int code;
  84.        struct
  85.      {
  86.        unsigned int anop:2;
  87.        #define    op    ldst.anop
  88.        unsigned int anrd:5;
  89.        #define    rd    ldst.anrd
  90.        unsigned int op3:6;
  91.        unsigned int anrs1:5;
  92.        #define    rs1    ldst.anrs1
  93.        unsigned int i:1;
  94.        unsigned int anasi:8;
  95.        #define    asi    ldst.anasi
  96.        unsigned int anrs2:5;
  97.        #define    rs2    ldst.anrs2
  98.        #define    shcnt    rs2
  99.      } ldst;
  100.        struct
  101.      {
  102.        unsigned int anop:2, anrd:5, op3:6, anrs1:5, i:1;
  103.        unsigned int IMM13:13;
  104.        #define    imm13    IMM13.IMM13
  105.      } IMM13;
  106.        struct
  107.      {
  108.        unsigned int anop:2;
  109.        unsigned int a:1;
  110.        unsigned int cond:4;
  111.        unsigned int op2:3;
  112.        unsigned int DISP22:22;
  113.        #define    disp22    branch.DISP22
  114.        #define    imm22    disp22
  115.      } branch;
  116.      #ifndef NO_V9
  117.        struct
  118.      {
  119.        unsigned int anop:2;
  120.        unsigned int a:1;
  121.        unsigned int z:1;
  122.        unsigned int rcond:3;
  123.        unsigned int op2:3;
  124.        unsigned int DISP16HI:2;
  125.        unsigned int p:1;
  126.        unsigned int _rs1:5;
  127.        unsigned int DISP16LO:14;
  128.      } branch16;
  129.      #endif
  130.        struct
  131.      {
  132.        unsigned int anop:2;
  133.        unsigned int adisp30:30;
  134.        #define    disp30    call.adisp30
  135.      } call;
  136.      };
  137.  
  138.    */
  139.  
  140. /* Nonzero if INSN is the opcode for a delayed branch.  */
  141. static int
  142. is_delayed_branch (insn)
  143.      unsigned long insn;
  144. {
  145.   unsigned int i;
  146.  
  147.   for (i = 0; i < NUMOPCODES; ++i)
  148.     {
  149.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  150.       if ((opcode->match & insn) == opcode->match
  151.       && (opcode->lose & insn) == 0)
  152.     return (opcode->flags & F_DELAYED);
  153.     }
  154.   return 0;
  155. }
  156.  
  157. static int opcodes_sorted = 0;
  158. /* extern void qsort (); */
  159. static int compare_opcodes ();
  160.  
  161. /* Print one instruction from MEMADDR on INFO->STREAM.
  162.  
  163.    We suffix the instruction with a comment that gives the absolute
  164.    address involved, as well as its symbolic form, if the instruction
  165.    is preceded by a findable `sethi' and it either adds an immediate
  166.    displacement to that register, or it is an `add' or `or' instruction
  167.    on that register.  */
  168. int
  169. print_insn_sparc (memaddr, info)
  170.      bfd_vma memaddr;
  171.      disassemble_info *info;
  172. {
  173.   FILE *stream = info->stream;
  174.   bfd_byte buffer[4];
  175.   unsigned long insn;
  176.   register unsigned int i;
  177.  
  178.   if (!opcodes_sorted)
  179.     {
  180.       qsort ((char *) sparc_opcodes, NUMOPCODES,
  181.          sizeof (sparc_opcodes[0]), compare_opcodes);
  182.       opcodes_sorted = 1;
  183.     }
  184.  
  185.   {
  186.     int status =
  187.       (*info->read_memory_func) (memaddr, buffer, sizeof (buffer), info);
  188.     if (status != 0)
  189.       {
  190.     (*info->memory_error_func) (status, memaddr, info);
  191.     return -1;
  192.       }
  193.   }
  194.  
  195.   insn = bfd_getb32 (buffer);
  196.  
  197.   info->insn_info_valid = 1;            /* We do return this info */
  198.   info->insn_type = dis_nonbranch;        /* Assume non branch insn */
  199.   info->branch_delay_insns = 0;            /* Assume no delay */
  200.   info->target = 0;                /* Assume no target known */
  201.  
  202.   for (i = 0; i < NUMOPCODES; ++i)
  203.     {
  204.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  205.       if ((opcode->match & insn) == opcode->match
  206.       && (opcode->lose & insn) == 0)
  207.     {
  208.       /* Nonzero means that we have found an instruction which has
  209.          the effect of adding or or'ing the imm13 field to rs1.  */
  210.       int imm_added_to_rs1 = 0;
  211.  
  212.       /* Nonzero means that we have found a plus sign in the args
  213.          field of the opcode table.  */
  214.       int found_plus = 0;
  215.       
  216.       /* Nonzero means we have an annulled branch.  */
  217.       int is_annulled = 0;
  218.  
  219.       /* Do we have an `add' or `or' instruction where rs1 is the same
  220.          as rsd, and which has the i bit set?  */
  221.       if ((opcode->match == 0x80102000 || opcode->match == 0x80002000)
  222.       /*              (or)                 (add)  */
  223.           && X_RS1 (insn) == X_RD (insn))
  224.         imm_added_to_rs1 = 1;
  225.  
  226.       if (X_RS1 (insn) != X_RD (insn)
  227.           && strchr (opcode->args, 'r') != 0)
  228.           /* Can't do simple format if source and dest are different.  */
  229.           continue;
  230.  
  231.       (*info->fprintf_func) (stream, opcode->name);
  232.  
  233.       {
  234.         register CONST char *s;
  235.  
  236.         if (opcode->args[0] != ',')
  237.           (*info->fprintf_func) (stream, " ");
  238.         for (s = opcode->args; *s != '\0'; ++s)
  239.           {
  240.         while (*s == ',')
  241.           {
  242.             (*info->fprintf_func) (stream, ",");
  243.             ++s;
  244.             switch (*s) {
  245.             case 'a':
  246.               (*info->fprintf_func) (stream, "a");
  247.               is_annulled = 1;
  248.               ++s;
  249.               continue;
  250. #ifndef NO_V9
  251.             case 'N':
  252.               (*info->fprintf_func) (stream, "pn");
  253.               ++s;
  254.               continue;
  255.  
  256.             case 'T':
  257.               (*info->fprintf_func) (stream, "pt");
  258.               ++s;
  259.               continue;
  260. #endif    /* NO_V9 */
  261.  
  262.             default:
  263.               break;
  264.             }        /* switch on arg */
  265.           }        /* while there are comma started args */
  266.  
  267.         (*info->fprintf_func) (stream, " ");
  268.             
  269.         switch (*s)
  270.           {
  271.           case '+':
  272.             found_plus = 1;
  273.  
  274.             /* note fall-through */
  275.           default:
  276.             (*info->fprintf_func) (stream, "%c", *s);
  277.             break;
  278.  
  279.           case '#':
  280.             (*info->fprintf_func) (stream, "0");
  281.             break;
  282.  
  283. #define    reg(n)    (*info->fprintf_func) (stream, "%%%s", reg_names[n])
  284.           case '1':
  285.           case 'r':
  286.             reg (X_RS1 (insn));
  287.             break;
  288.  
  289.           case '2':
  290.             reg (X_RS2 (insn));
  291.             break;
  292.  
  293.           case 'd':
  294.             reg (X_RD (insn));
  295.             break;
  296. #undef    reg
  297.  
  298. #define    freg(n)        (*info->fprintf_func) (stream, "%%%s", freg_names[n])
  299. #define    fregx(n)    (*info->fprintf_func) (stream, "%%%s", freg_names[((n) & ~1) | (((n) & 1) << 5)])
  300.           case 'e':
  301.             freg (X_RS1 (insn));
  302.             break;
  303.           case 'v':    /* double/even */
  304.           case 'V':    /* quad/multiple of 4 */
  305.             fregx (X_RS1 (insn));
  306.             break;
  307.  
  308.           case 'f':
  309.             freg (X_RS2 (insn));
  310.             break;
  311.           case 'B':    /* double/even */
  312.           case 'R':    /* quad/multiple of 4 */
  313.             fregx (X_RS2 (insn));
  314.             break;
  315.  
  316.           case 'g':
  317.             freg (X_RD (insn));
  318.             break;
  319.           case 'H':    /* double/even */
  320.           case 'J':    /* quad/multiple of 4 */
  321.             fregx (X_RD (insn));
  322.             break;
  323. #undef    freg
  324. #undef    fregx
  325.  
  326. #define    creg(n)    (*info->fprintf_func) (stream, "%%c%u", (unsigned int) (n))
  327.           case 'b':
  328.             creg (X_RS1 (insn));
  329.             break;
  330.  
  331.           case 'c':
  332.             creg (X_RS2 (insn));
  333.             break;
  334.  
  335.           case 'D':
  336.             creg (X_RD (insn));
  337.             break;
  338. #undef    creg
  339.  
  340.           case 'h':
  341.             (*info->fprintf_func) (stream, "%%hi(%#x)",
  342.                        (0xFFFFFFFF
  343.                         & ((int) X_IMM22 (insn) << 10)));
  344.             break;
  345.  
  346.           case 'i':
  347.             {
  348.               int imm = SEX (X_IMM13 (insn), 13);
  349.  
  350.               /* Check to see whether we have a 1+i, and take
  351.              note of that fact.
  352.  
  353.              Note: because of the way we sort the table,
  354.              we will be matching 1+i rather than i+1,
  355.              so it is OK to assume that i is after +,
  356.              not before it.  */
  357.               if (found_plus)
  358.             imm_added_to_rs1 = 1;
  359.               
  360.               if (imm <= 9)
  361.             (*info->fprintf_func) (stream, "%d", imm);
  362.               else
  363.             (*info->fprintf_func) (stream, "%#x", imm);
  364.             }
  365.             break;
  366.  
  367. #ifndef NO_V9
  368.           case 'I':    /* 11 bit immediate.  */
  369.           case 'j':    /* 10 bit immediate.  */
  370.             {
  371.               int imm;
  372.  
  373.               if (*s == 'I')
  374.             imm = SEX (X_IMM13 (insn), 11);
  375.               else
  376.             imm = SEX (X_IMM13 (insn), 10);
  377.  
  378.               /* Check to see whether we have a 1+i, and take
  379.              note of that fact.
  380.              
  381.              Note: because of the way we sort the table,
  382.              we will be matching 1+i rather than i+1,
  383.              so it is OK to assume that i is after +,
  384.              not before it.  */
  385.               if (found_plus)
  386.             imm_added_to_rs1 = 1;
  387.               
  388.               if (imm <= 9)
  389.             (info->fprintf_func) (stream, "%d", imm);
  390.               else
  391.             (info->fprintf_func) (stream, "%#x", (unsigned) imm);
  392.             }
  393.             break;
  394.  
  395.           case 'k':
  396.             info->target = memaddr + (SEX (X_DISP16 (insn), 16)) * 4;
  397.             (*info->print_address_func) (info->target, info);
  398.             break;
  399.  
  400.           case 'G':
  401.             info->target = memaddr + (SEX (X_DISP22 (insn), 19)) * 4;
  402.             (*info->print_address_func) (info->target, info);
  403.             break;
  404.  
  405.           case '6':
  406.           case '7':
  407.           case '8':
  408.           case '9':
  409.             (*info->fprintf_func) (stream, "%%fcc%c", *s - '6' + '0');
  410.             break;
  411.  
  412.           case 'z':
  413.             (*info->fprintf_func) (stream, "%%icc");
  414.             break;
  415.  
  416.           case 'Z':
  417.             (*info->fprintf_func) (stream, "%%xcc");
  418.             break;
  419.  
  420.           case 'E':
  421.             (*info->fprintf_func) (stream, "%%ccr");
  422.             break;
  423.  
  424.           case 's':
  425.             (*info->fprintf_func) (stream, "%%fprs");
  426.             break;
  427.  
  428.           case 'o':
  429.             (*info->fprintf_func) (stream, "%%asi");
  430.             break;
  431.  
  432.           case 'W':
  433.             (*info->fprintf_func) (stream, "%%tick");
  434.             break;
  435.  
  436.           case 'P':
  437.             (*info->fprintf_func) (stream, "%%pc");
  438.             break;
  439.  
  440.           case '?':
  441.             if (X_RS1 (insn) == 31)
  442.               (*info->fprintf_func) (stream, "%%ver");
  443.             else if ((unsigned) X_RS1 (insn) < 16)
  444.               (*info->fprintf_func) (stream, "%%%s",
  445.                          v9_priv_reg_names[X_RS1 (insn)]);
  446.             else
  447.               (*info->fprintf_func) (stream, "%%reserved");
  448.             break;
  449.  
  450.           case '!':
  451.             if ((unsigned) X_RD (insn) < 15)
  452.               (*info->fprintf_func) (stream, "%%%s",
  453.                          v9_priv_reg_names[X_RD (insn)]);
  454.             else
  455.               (*info->fprintf_func) (stream, "%%reserved");
  456.             break;
  457.             break;
  458. #endif    /* NO_V9 */
  459.  
  460.           case 'M':
  461.             (*info->fprintf_func) (stream, "%%asr%d", X_RS1 (insn));
  462.             break;
  463.             
  464.           case 'm':
  465.             (*info->fprintf_func) (stream, "%%asr%d", X_RD (insn));
  466.             break;
  467.             
  468.           case 'L':
  469.             info->target = memaddr + X_DISP30 (insn) * 4;
  470.             (*info->print_address_func) (info->target, info);
  471.             break;
  472.  
  473.           case 'n':
  474.             (*info->fprintf_func)
  475.               (stream, "%#x", (SEX (X_DISP22 (insn), 22)));
  476.             break;
  477.  
  478.           case 'l':
  479.             info->target = memaddr + (SEX (X_DISP22 (insn), 22)) * 4;
  480.             (*info->print_address_func) (info->target, info);
  481.             break;
  482.  
  483.           case 'A':
  484.             (*info->fprintf_func) (stream, "(%d)", X_ASI (insn));
  485.             break;
  486.  
  487.           case 'C':
  488.             (*info->fprintf_func) (stream, "%%csr");
  489.             break;
  490.  
  491.           case 'F':
  492.             (*info->fprintf_func) (stream, "%%fsr");
  493.             break;
  494.  
  495.           case 'p':
  496.             (*info->fprintf_func) (stream, "%%psr");
  497.             break;
  498.  
  499.           case 'q':
  500.             (*info->fprintf_func) (stream, "%%fq");
  501.             break;
  502.  
  503.           case 'Q':
  504.             (*info->fprintf_func) (stream, "%%cq");
  505.             break;
  506.  
  507.           case 't':
  508.             (*info->fprintf_func) (stream, "%%tbr");
  509.             break;
  510.  
  511.           case 'w':
  512.             (*info->fprintf_func) (stream, "%%wim");
  513.             break;
  514.  
  515.           case 'x':
  516.             (*info->fprintf_func) (stream, "%d",
  517.                        ((X_LDST_I (insn) << 8)
  518.                         + X_ASI (insn)));
  519.             break;
  520.  
  521.           case 'y':
  522.             (*info->fprintf_func) (stream, "%%y");
  523.             break;
  524.           }
  525.           }
  526.       }
  527.  
  528.       /* If we are adding or or'ing something to rs1, then
  529.          check to see whether the previous instruction was
  530.          a sethi to the same register as in the sethi.
  531.          If so, attempt to print the result of the add or
  532.          or (in this context add and or do the same thing)
  533.          and its symbolic value.  */
  534.       if (imm_added_to_rs1)
  535.         {
  536.           unsigned long prev_insn;
  537.           int errcode;
  538.  
  539.           errcode =
  540.         (*info->read_memory_func)
  541.           (memaddr - 4, buffer, sizeof (buffer), info);
  542.           prev_insn = bfd_getb32 (buffer);
  543.  
  544.           if (errcode == 0)
  545.         {
  546.           /* If it is a delayed branch, we need to look at the
  547.              instruction before the delayed branch.  This handles
  548.              sequences such as
  549.  
  550.              sethi %o1, %hi(_foo), %o1
  551.              call _printf
  552.              or %o1, %lo(_foo), %o1
  553.              */
  554.  
  555.           if (is_delayed_branch (prev_insn))
  556.             {
  557.               errcode = (*info->read_memory_func)
  558.             (memaddr - 8, buffer, sizeof (buffer), info);
  559.               prev_insn = bfd_getb32 (buffer);
  560.             }
  561.         }
  562.  
  563.           /* If there was a problem reading memory, then assume
  564.          the previous instruction was not sethi.  */
  565.           if (errcode == 0)
  566.         {
  567.           /* Is it sethi to the same register?  */
  568.           if ((prev_insn & 0xc1c00000) == 0x01000000
  569.               && X_RD (prev_insn) == X_RS1 (insn))
  570.             {
  571.               (*info->fprintf_func) (stream, "\t! ");
  572.               info->target = 
  573.             (0xFFFFFFFF & (int) X_IMM22 (prev_insn) << 10)
  574.             | SEX (X_IMM13 (insn), 13);
  575.               (*info->print_address_func) (info->target, info);
  576.               info->insn_type = dis_dref;
  577.               info->data_size = 4;  /* FIXME!!! */
  578.             }
  579.         }
  580.         }
  581.  
  582.       if (opcode->flags & (F_UNBR|F_CONDBR|F_JSR))
  583.         {
  584.         /* FIXME -- check is_annulled flag */
  585.           if (opcode->flags & F_UNBR)
  586.         info->insn_type = dis_branch;
  587.           if (opcode->flags & F_CONDBR)
  588.         info->insn_type = dis_condbranch;
  589.           if (opcode->flags & F_JSR)
  590.         info->insn_type = dis_jsr;
  591.           if (opcode->flags & F_DELAYED)
  592.         info->branch_delay_insns = 1;
  593.         }
  594.  
  595.       return sizeof (buffer);
  596.     }
  597.     }
  598.  
  599.   info->insn_type = dis_noninsn;    /* Mark as non-valid instruction */
  600.   (*info->fprintf_func) (stream, "%#8x", insn);
  601.   return sizeof (buffer);
  602. }
  603.  
  604. /* Compare opcodes A and B.  */
  605.  
  606. static int
  607. compare_opcodes (a, b)
  608.      char *a, *b;
  609. {
  610.   struct sparc_opcode *op0 = (struct sparc_opcode *) a;
  611.   struct sparc_opcode *op1 = (struct sparc_opcode *) b;
  612.   unsigned long int match0 = op0->match, match1 = op1->match;
  613.   unsigned long int lose0 = op0->lose, lose1 = op1->lose;
  614.   register unsigned int i;
  615.  
  616.   /* If a bit is set in both match and lose, there is something
  617.      wrong with the opcode table.  */
  618.   if (match0 & lose0)
  619.     {
  620.       fprintf (stderr, "Internal error:  bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  621.            op0->name, match0, lose0);
  622.       op0->lose &= ~op0->match;
  623.       lose0 = op0->lose;
  624.     }
  625.  
  626.   if (match1 & lose1)
  627.     {
  628.       fprintf (stderr, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  629.            op1->name, match1, lose1);
  630.       op1->lose &= ~op1->match;
  631.       lose1 = op1->lose;
  632.     }
  633.  
  634.   /* Because the bits that are variable in one opcode are constant in
  635.      another, it is important to order the opcodes in the right order.  */
  636.   for (i = 0; i < 32; ++i)
  637.     {
  638.       unsigned long int x = 1 << i;
  639.       int x0 = (match0 & x) != 0;
  640.       int x1 = (match1 & x) != 0;
  641.  
  642.       if (x0 != x1)
  643.     return x1 - x0;
  644.     }
  645.  
  646.   for (i = 0; i < 32; ++i)
  647.     {
  648.       unsigned long int x = 1 << i;
  649.       int x0 = (lose0 & x) != 0;
  650.       int x1 = (lose1 & x) != 0;
  651.  
  652.       if (x0 != x1)
  653.     return x1 - x0;
  654.     }
  655.  
  656.   /* They are functionally equal.  So as long as the opcode table is
  657.      valid, we can put whichever one first we want, on aesthetic grounds.  */
  658.  
  659.   /* Our first aesthetic ground is that aliases defer to real insns.  */
  660.   {
  661.     int alias_diff = (op0->flags & F_ALIAS) - (op1->flags & F_ALIAS);
  662.     if (alias_diff != 0)
  663.       /* Put the one that isn't an alias first.  */
  664.       return alias_diff;
  665.   }
  666.  
  667.   /* Except for aliases, two "identical" instructions had
  668.      better have the same opcode.  This is a sanity check on the table.  */
  669.   i = strcmp (op0->name, op1->name);
  670.   if (i)
  671.       if (op0->flags & F_ALIAS) /* If they're both aliases, be arbitrary. */
  672.       return i;
  673.       else
  674.       fprintf (stderr,
  675.            "Internal error: bad sparc-opcode.h: \"%s\" == \"%s\"\n",
  676.            op0->name, op1->name);
  677.  
  678.   /* Fewer arguments are preferred.  */
  679.   {
  680.     int length_diff = strlen (op0->args) - strlen (op1->args);
  681.     if (length_diff != 0)
  682.       /* Put the one with fewer arguments first.  */
  683.       return length_diff;
  684.   }
  685.  
  686.   /* Put 1+i before i+1.  */
  687.   {
  688.     char *p0 = (char *) strchr(op0->args, '+');
  689.     char *p1 = (char *) strchr(op1->args, '+');
  690.  
  691.     if (p0 && p1)
  692.       {
  693.     /* There is a plus in both operands.  Note that a plus
  694.        sign cannot be the first character in args,
  695.        so the following [-1]'s are valid.  */
  696.     if (p0[-1] == 'i' && p1[1] == 'i')
  697.       /* op0 is i+1 and op1 is 1+i, so op1 goes first.  */
  698.       return 1;
  699.     if (p0[1] == 'i' && p1[-1] == 'i')
  700.       /* op0 is 1+i and op1 is i+1, so op0 goes first.  */
  701.       return -1;
  702.       }
  703.   }
  704.  
  705.   /* They are, as far as we can tell, identical.
  706.      Since qsort may have rearranged the table partially, there is
  707.      no way to tell which one was first in the opcode table as
  708.      written, so just say there are equal.  */
  709.   return 0;
  710. }
  711.